// buyordersure.js
var app = getApp()
var host = app.globalData.host
Page({
  data: {
    spuId:'',
    buynumber:'',
    whichcolor:'',
    whichsize:'',
    ordercontent:'',
    openid:'',
    image: app.globalData.image,
    whichcolorname:'',
    whichsizename:'',
    totalPrice:'',
    array: [],
    keyArray: [],
    index: 0,
    remarks:'',
    addressId: '',
    pageempty:true,
    pageunempty:'',
    addrId: '',
    recipientName: '',
    recipientMobile: '',
    recipientAddress: ''
  },
  changeRemarks: function (e) {
    var that = this
    this.setData({
      remarks: e.detail.value
    })
  },
  onLoad: function (options) {
    var that = this;
    wx.removeStorageSync("selectedAddr")
    this.setData({
      spuId: options.id,
      buynumber: options.buynumber,
      whichcolor: options.whichcolor,
      whichcolorname: options.whichcolorname,
      whichsize: options.whichsize,
      whichsizename: options.whichsizename
    })
    wx.request({
      url: host + '/spuApi/spuProductDetail?id=' + that.data.spuId,
      header: { 'content-type': 'application/json' },
      success:function(res){
        var totalPrice = res.data.data.price * that.data.buynumber;
        that.setData({
          ordercontent:res.data.data,
          totalPrice:totalPrice
        })      
      }
    })
  },
  onShow: function () {
    var that = this;
    var selectAddr = wx.getStorageSync("selectedAddr")
    if (selectAddr) {
      wx.removeStorageSync("selectedAddr")
      console.info("remove storaage")
      this.setData({
        pageempty:'',
        pageunempty:true,
        addrId: selectAddr.id,
        recipientName: selectAddr.name,
        recipientMobile: selectAddr.mobile,
        recipientAddress: selectAddr.province + selectAddr.city + selectAddr.county + selectAddr.address
      })
    } else {
      wx.request({
        url: host + "/addressApi/selAddress",
        data: {
          thirdpartyId: wx.getStorageSync('openId')
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          var addrArr = res.data.data
          if (addrArr && addrArr.length > 0){
            var defaultAddr = addrArr[0];//默认地址
            if (defaultAddr){
              that.setData({
                pageempty: '',
                pageunempty: true,
                addrId: defaultAddr.id,
                recipientName: defaultAddr.name,
                recipientMobile: defaultAddr.mobile,
                recipientAddress: defaultAddr.province + defaultAddr.city + defaultAddr.county + defaultAddr.address
              })
            } else {
              that.setData({
                pageempty: true,
                pageunempty: '',
                addrId: '',
                recipientName: '',
                recipientMobile: '',
                recipientAddress: ''
              })
            }
          } else {
            that.setData({
              pageempty: true,
              pageunempty: '',
              addrId: '',
              recipientName: '',
              recipientMobile: '',
              recipientAddress: ''
            })
          }
        }
      })
    }
  },
  bindPickerChange: function (e) {
    var that = this
    this.setData({
      index: e.detail.value,
      addressId: that.data.keyArray[e.detail.value]
    })
  },
  topay:function(){
    var that = this
    if (that.data.pageempty == true){
      wx.showToast({
        title: '请先添加收货地址',
        icon: 'success',
        duration: 2000
      })
    }else{
      wx.request({
        url: host + '/order/addOrderDirect',
        header: { 'content-type': 'application/json' },
        data: {
          openId: wx.getStorageSync('openId'),
          spuId: that.data.spuId,
          productNum: that.data.buynumber,
          addressId: that.data.addrId,
          remarks: that.data.remarks,
          productSaleAttrsSplitWithComma: that.data.whichcolor + ',' + that.data.whichsize
        },
        success: function (res) {
          var sour = res.data.data
          wx.setStorageSync("to_pay_order_id", sour.orderId)
          wx.requestPayment({
            'timeStamp': sour.timeStamp,
            'nonceStr': sour.nonce_str,
            'package': sour.package,
            'signType': sour.signType,
            'paySign': sour.paySign,
            'success': function (res) {
              console.info("pay suc " + res)
            },
            'fail': function (res) {
              console.info("pay fail " + res)
            }, 'complete': function (res) {
              
              wx.redirectTo({
                url: '../orderdetail/orderdetail?depth=2&id=' + wx.getStorageSync("to_pay_order_id")
              })
            }
          })
        }
      })
    }
  },
  toaddress:function(){
    wx.navigateTo({
      url: '../personaddress/personaddress?comefrom=weborder',
    })
  }
})